package sky.wicket.panels;

import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sky.domain.Country;
import sky.wicket.AddUpdateCountry;
import sky.wicket.CountryInfo;
import sky.wicket.HomePage;
import sky.wicket.MyWicketUtils;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 17.11.13
 * Time: 17:43
 * To change this template use File | Settings | File Templates.
 */
public class CountryPanel extends Panel {
    static final Logger log = LoggerFactory.getLogger(CountryPanel.class);

    public CountryPanel(String id) {
        super(id);
        throw new RuntimeException("Mistaken Constructor");
    }

    public CountryPanel(String id, IModel<?> model) {
        super(id, model);
        Country country;
        try {
            country = (Country) model.getObject();
        } catch (ClassCastException e) {
            log.error("Cast exception. Expected Country.class but found" + model.getObject().getClass().toString() + ".class");
            throw new RuntimeException(e);
        }
        addElementsToPanel(country);
    }

    private void addElementsToPanel(final Country country) {
        //info about country
        MyWicketUtils.addBookmarkablePageLinkWithLabelAndParameters(this, "countryNameLink", CountryInfo.class,
                new PageParameters().add("countryName", country.getName()), "countryNameLabel", country.getName());
        //edit airport link
        Link editCountryLink = new Link("editCountryLink") {
            @Override
            public void onClick() {
                setResponsePage(new AddUpdateCountry(country.getName()));
            }
        };
        editCountryLink.add(new Label("editCountryLabel", "[7]"));
        this.add(editCountryLink);
        //remove airport link
        Link removeCountryLink = new Link("removeCountryLink") {
            @Override
            public void onClick() {
                //remove airport
            }
        };
        removeCountryLink.add(new Label("removeCountryLabel", "[x]"));
        this.add(removeCountryLink);
    }
}
